package com.medical.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.medical.config.SystemContext;
import com.medical.dao.SysMbzcMapper;
import com.medical.entity.SysMbzc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

@Service
@Transactional
public class MbzcService {
    @Autowired
    private SysMbzcMapper sysMbzcMapper;


    /**
     * 根据用户ID查找用户信息
     * @param mbzcId
     * @return
     */
    public SysMbzc findMbzcByMbzcId(Integer mbzcId) {
        return sysMbzcMapper.findMbzcByMbzcId(mbzcId);
    }

    /**
     * 查找用户列表
     * @return
     */
    public PageInfo<SysMbzc> getMbzcList(String mbzcName, Integer currentPage, Integer limit){
        //利用PageHelper分页查询 注意：这个一定要放查询语句的前一行,否则无法进行分页,因为它对紧随其后第一个sql语句有效
        PageHelper.startPage(currentPage, limit);
        List<SysMbzc> mbzcList = sysMbzcMapper.getMbzcList(mbzcName);
        PageInfo<SysMbzc> pageInfo = new PageInfo<SysMbzc>(mbzcList);
        return pageInfo;
    }

    /**
     * 根据ID获取用户信息
     * @param id
     * @return
     */
    public SysMbzc getMbzcById(Integer id) {
        return sysMbzcMapper.selectByPrimaryKey(id);
    }

    /**
     * 添加
     * @param sysMbzc
     */
    public void addMbzc(SysMbzc sysMbzc) {
        sysMbzc.setCreateTime(new Timestamp(System.currentTimeMillis()));
        sysMbzc.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        sysMbzc.setmbzcOther(SystemContext.passwdEncryption(sysMbzc.getmbzcOther()));

        sysMbzcMapper.insert(sysMbzc);
    }

    /**
     * 更新
     * @param sysMbzc
     */
    public void edit(SysMbzc sysMbzc) {
        sysMbzc.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        sysMbzcMapper.updateByPrimaryKeySelective(sysMbzc);
    }

    /**
     * 删除
     * @param id
     */
    public void delete(Integer id) {
        sysMbzcMapper.deleteByPrimaryKey(id);
    }
}
